package cn.edu.buaa.cnsatm.traffic_big_data_server.railway.dao;

import cn.edu.buaa.cnsatm.traffic_big_data_server.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;

@Repository
public class RailwayStatusDAO {
    public ResultSet getTopKRailwayWeatherEffect(int k){
        /**
         * @author: LX
         * @methodName: getTopKRailwayWeatherEffect
         * @description: 查询前k条铁路气象影响
         * @data: 2019-11-05
         **/
        String sql = "SELECT block_reason, block_description, block_start_time,block_title FROM road_status WHERE block_reason = '气象' AND road_type = '铁路' ORDER BY block_start_time DESC LIMIT " + String.valueOf(k);
        Statement stmt = null;
        ResultSet resultSet = null;
        try {
            stmt = JDBCUtil.databaseConnecttion.createStatement();
            resultSet= stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
    public ArrayList<ArrayList<String>> getStatusStrList(Integer itemNum) throws SQLException {
        ArrayList<ArrayList<String>> statusList=new ArrayList<ArrayList<String>>();
        String sql="select *"
                + " from railway_statu"
                + " ORDER BY datetime DESC"
                + " LIMIT "+itemNum;
        //JDBCUtil.databaseConnecttion，若未请求到则为空，导致空指针异常
        if(JDBCUtil.databaseConnecttion==null) throw new SQLException();
        Statement stmt = JDBCUtil.databaseConnecttion.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            ArrayList<String> temp=new ArrayList<String>();
            String datetime = rs.getString("datetime");
            temp.add(datetime.substring(0,datetime.length()-3));
            temp.add(rs.getString("info"));
            temp.add(rs.getString("description"));
            statusList.add(temp);
        }
        return statusList;
    }
}
